package com.guoxue.action.back;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.guoxue.action.front.BaseAction;
import com.guoxue.domain.PublishLogsVo;
import com.guoxue.domain.UpdateTableVo;
import com.guoxue.service.IPublishLogsService;
import com.guoxue.service.IUpdateService;
import com.guoxue.util.JsonUtil;
import com.guoxue.util.LoggerUtil;
/**
 * 发布打包版本相关操作类
 * @author young
 */
@Controller
@RequestMapping("publish")
public class PublishAction extends BaseAction{
	
	@Resource
	private IPublishLogsService publishService;
	@Resource
	private IUpdateService updateService;
	/**
	 *  发布版本
	 * @param req
	 * @param resp
	 * @param versionCode
	 * @return
	 */
	@RequestMapping("/push")
	@ResponseBody
	public JSON push(HttpServletRequest req, HttpServletResponse resp, String versionCode){
		try {
			if ("".equals(versionCode)||versionCode==null){ 
				return Message(ERROR, "版本发布错误", null);
			} else {
				publishService.updateStatus(versionCode);
				return Message(SUCCESS, "版本发行成功", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"版本发布异常");
		}
	}
	/**
	 *  检查更新接口
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping("/checkForUpdate")
	@ResponseBody
	public JSON checkForUpdate(HttpServletRequest req, HttpServletResponse resp){
		try {
			PublishLogsVo pl = publishService.getPublishByStatus("4");
			if (pl!=null){
				return Message(SUCCESS, "获取当前版本信息", pl);
			}else{
				return Message(ERROR, "获取版本信息失败", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"获取版本信息异常");
		}
	}
	/**
	 * 版本更新下载接口
	 * @param req
	 * @param resp
	 * @param versionCode
	 * @return
	 */
	@RequestMapping("/downloadForUpdate")
	@ResponseBody
	public JSON downloadForUpdate(HttpServletRequest req, HttpServletResponse resp, String versionCode){
		try {
			if (versionCode==null||"".equals(versionCode)){
				return Message(ERROR, "版本号不能为空", null);
			} else {
				List<UpdateTableVo> datas = updateService.getUpdateTableList(versionCode);
				if (datas.isEmpty()){
					return Message(ERROR, "获取更新数据空", null);
				} else {
					return Message(SUCCESS, "获取成功", datas);
				}
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"获取版本信息异常");
		}
	}
	
}
